<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客详情页</title>

    <link rel="stylesheet" href="CSS/common.css">
    <link rel="stylesheet" href="CSS/blog_detail.css">

     <!-- 引入 editor.md 的依赖 -->
     <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
     <script src="JS/jquery.min.js"></script>
     <script src="editor.md/lib/marked.min.js"></script>
     <script src="editor.md/lib/prettify.min.js"></script>
     <script src="editor.md/editormd.js"></script>
</head>
<body>
    <div class="nav">
        <!-- 导航栏 -->
        <img src="image/123.PNG" alt="加载失败">
        <span class="title">我的博客系统</span>
        <span class = "spacer"></span>

        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logOut">注销</a>
    </div>

    <!-- 页面主体 -->
    <div class = "container">
        <div class="container-left">
            <div class="card">
                <img src="image/456.JPG" alt="">
                <h3>温习文</h3>
                <a href="https://gitee.com/wenxiwen19981229" target="_blank">github地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>

                <div class="counter">
                    <span class="article">2</span>
                    <span>3</span>
                </div>
            </div>
        </div>

        <div class="container-right">
            <div class="blog-content">
                <h3>我的第一篇博客</h3>

                <!-- 日期 -->
                <div class="data">2022.9.30 12:00:00</div>

                <!-- 正文 -->
                <div id="content" style="opacity: 50%">
                    
                </div>
            </div>
        </div>
    </div>
            <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>  
            <script src="JS/app.js"></script>

            <script>
                    function getBlog() {

                    $.ajax({

                        type: 'get',
                        url: 'blog' + location.search,

                        success: function(body) {
                            let h3 = document.querySelector(".blog-content h3");
                            h3.innerHTML = body.title;

                            let Divdata = document.querySelector(".blog-content .data");
                            Divdata.innerHTML = body.postTime;

                            //这种写法是直接写入标签,并不带有渲染效果
                            // let Divcontent = document.querySelector("#content");
                            // Divcontent.innerHTML = body.content;
                            
                            //markdownToHTML是editor.md自带的方法,就是用来把字符串转换成格式化效果的
                            //第一个参数就是你要存放格式化(渲染)内容的标签,
                            //第二个参数是一个js对象,直接把正文内容传入即可
                            editormd.markdownToHTML('content',{
                                markdown: body.content
                            });
                        }
                    });
                }
                //显示博客详情
                getBlog();
                //登录验证
                checkLogin();

                 function getUserInfo() {

                        $.ajax({
                            type: 'get',
                            url: 'userInfo' + location.search,

                            success: function(body) {
                                let h3 = document.querySelector(".card h3");
                                h3.innerHTML = body.userName;
                                
                                //如果发现博客的作者和登录用户是同一人就显示删除按钮,并返回要删除的文章ID.否则就不显示
                                if(body.isYourBlog) {
                                    let deleteA = document.createElement("a");
                                    deleteA.href = "blogDelete" + location.search;
                                    deleteA.innerHTML = '删除';

                                    let navDiv = document.querySelector('.nav');
                                    navDiv.appendChild(deleteA);
                                }
                            }
                            
                        });
                        
                    }

                    //显示用户信息
                    getUserInfo();
                    
                    function getUserArticle(){
                        $.ajax({
                            type: 'post',
                            url: 'userInfo' + location.search,

                            success: function(body) {
                                let articleDiv = document.querySelector(".counter .article");
                                articleDiv.innerHTML = body;
                            }
                        });
                    }

                    //显示用户博文章数
                    getUserArticle();
            </script>
</body>
</html>